package com.iskyonline.skyonlieapp.service;

import com.iskyonline.skyonlieapp.entity.KeyValue;
import com.iskyonline.skyonlieapp.mapper.KeyValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndexService {

    @Autowired
    private KeyValueMapper keyValueMapper;

    @Value("${file.image.url}")
    protected String fileUrl;

    public void saveIndexPicture(String picFileName) {

        keyValueMapper.delKeyValue(1);
        KeyValue keyValue = new KeyValue();
        keyValue.setPicUrl(picFileName);
        keyValue.setValueType(1);
        keyValueMapper.insert(keyValue);
    }

    public void saveIndexBannerInfo(String picFileName, String picTitle) {
        final KeyValue keyValue = new KeyValue();
        keyValue.setPicUrl(picFileName);
        keyValue.setPicTitle(picTitle);
        keyValue.setValueType(2);
        keyValueMapper.insert(keyValue);
    }

    public void saveHeZuoHuoBan(String picFileName) {
        final KeyValue keyValue = new KeyValue();
        keyValue.setPicUrl(picFileName);
        keyValue.setValueType(3);
        keyValueMapper.insert(keyValue);
    }

    public List<KeyValue> listKeyValue(int valeuType) {
        final List<KeyValue> keyValues = keyValueMapper.listKeyValue(valeuType);
        System.out.println(">>>>>>>>>>>" + keyValues.size());
        for (KeyValue kv : keyValues) {
            kv.setPicUrl(fileUrl + kv.getPicUrl());
        }
        return keyValues;
    }

    public void delKeyValue(int id) {
        keyValueMapper.deleteByPrimaryKey(id);
    }

    public void saveWaiBao(String context, int valueType){
        keyValueMapper.delKeyValue(valueType);
        final KeyValue keyValue = new KeyValue();
        keyValue.setValueText(context);
        keyValue.setValueType(valueType);
        keyValueMapper.insert(keyValue);
    }

    public KeyValue loadKeyValueByType(int type) {
        return keyValueMapper.getKeyValue(type);
    }

    public void saveInfo(String picUrl, String address, String beianhao, String phone) {
        keyValueMapper.delKeyValue(9);
        final KeyValue keyValue = new KeyValue();
        keyValue.setPicUrl(picUrl);
        keyValue.setValueType(9);
        keyValue.setValueCase1(address);
        keyValue.setValueCase2(phone);
        keyValue.setValueCase3(beianhao);
        keyValueMapper.insert(keyValue);
    }
}
